﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web
{
    public partial class IList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id; if (int.TryParse(Request.QueryString["id"], out id))
                {
                    this.hidId.Value = id.ToString();
                    bindData(id);
                }
            }
        }

        private void bindData(int id)
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            System.Data.DataTable dt = dal.GetWebSiteI(id);
            if (dt.Rows.Count > 0)
            {
                this.litTitle.Text = dt.Rows[0]["Title"].ToString();
                this.litContent.Text = dt.Rows[0]["Content"].ToString();
                if (Convert.ToInt32(dt.Rows[0]["cansign"]) == 1)
                {
                    if (dal.GetWebSiteISubItemList(int.Parse(this.hidId.Value), null).Rows.Count > 0)
                    {
                        SubItemInfo.Visible = true;
                        HdSign.Visible = false;
                        bindGrid(id);
                    }
                    else
                    {
                        SubItemInfo.Visible = false;
                        HdSign.Visible = true;
                        bindSignInfo(id);
                    }
                }
                else
                {
                    SubItemInfo.Visible = false;
                    HdSign.Visible = false;
                }
                
            }
        }

        private void bindGrid(int id)
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.dgdSubItem.DataSource = dal.GetWebSiteISubItemList(id,null);
            this.dgdSubItem.DataBind();
            if (dal.GetWebSiteIisSelfSign(id))
            {
                this.cmdSubmit.Text = " 您已经报过名了！";
                this.cmdSubmit.Enabled = false;
                this.lblRem.Visible = false;
                this.txtRem.Visible = false;
            }
        }

        private void bindSignInfo(int id)
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            if (dal.GetWebSiteIisSelfSign(id))
            {
                this.cmdSign.Text = " 您已经报过名了！";
                this.cmdSign.Enabled = false;
                this.lblRem1.Visible = false;
                this.txtRem1.Visible = false;
            }
        }

        protected void cmdSubmit_Click(object sender, EventArgs e)
        {
            int subItemId;
            if (this.hidSubItemId.Value == string.Empty)
            {
                Common.JsAlert(this, "请选择分项！");
                return;
            }
            if (int.TryParse(this.hidSubItemId.Value, out subItemId))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                string rem = this.txtRem.Text.Trim();
                if (dal.AddWebSiteISign(int.Parse(this.hidId.Value), subItemId, rem))
                {
                    Common.JsAlert(this, "感谢您的参与！");
                    bindData(int.Parse(this.hidId.Value));
                }
            }
            else
            {
                Common.JsAlert(this, "参数错误！");
                return;
            }
        }

        protected void cmdSign_Click(object sender, EventArgs e)
        {
            int Id;
            if (int.TryParse(this.hidId.Value, out Id))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                string rem = this.txtRem1.Text.Trim();
                if (dal.AddWebSiteISign(Id, null, rem))
                {
                    Common.JsAlert(this, "感谢您的参与！");
                    bindData(Id);
                }
            }
            else
            {
                Common.JsAlert(this, "参数错误！");
                return;
            }
        }
    }
}